package com.meals.system.api.user;

import com.meals.system.api.user.param.InsertSystemUserAddressParam;
import com.meals.system.api.user.param.UpdateSystemUserAddressParam;
import com.meals.system.api.user.vo.WhitesMealUserAddressVO;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.config.annotation.TokenToSystemUser;
import com.meals.system.entity.SystemUser;
import com.meals.system.entity.SystemUserAddress;
import com.meals.system.service.WhitesMealUserAddressService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.Result;
import com.meals.system.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@RestController
@Api(value = "v1", tags = "3-3.白餐系统用户地址相关接口")
@RequestMapping("/user-api/v1")
public class WhitesMealUserAddressAPI {

    @Resource
    private WhitesMealUserAddressService whitesMealUserAddressService;

    /**
     * 获取登录用户的信息，返回登录用户的收货地址列表
     * @param loginSystemUser
     * @return
     */
    @GetMapping("/address")
    @ApiOperation(value = "我的收货地址列表", notes = "")
    public Result<List<WhitesMealUserAddressVO>> addressList(@TokenToSystemUser SystemUser loginSystemUser){
        return ResultGenerator.genSuccessResult(whitesMealUserAddressService.getLoginSystemUserAddress(loginSystemUser.getUserId()));
    }

    @PostMapping("/address")
    @ApiOperation(value = "添加地址", notes = "")
    public Result<Boolean> saveUserAddress(@RequestBody InsertSystemUserAddressParam insertSystemUserAddressParam,
                                           @TokenToSystemUser SystemUser loginSystemUser){
        SystemUserAddress systemUserAddress = new SystemUserAddress();
        BeanUtil.copyProperties(insertSystemUserAddressParam,systemUserAddress);
        systemUserAddress.setUserId(loginSystemUser.getUserId());
        Boolean saveResult = whitesMealUserAddressService.insertUserAddress(systemUserAddress);
        if (saveResult){
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }

    @PutMapping("/address")
    @ApiOperation(value = "修改地址", notes = "")
    public Result<Boolean> updateSystemUserAddress(@RequestBody UpdateSystemUserAddressParam updateSystemUserAddressParam,
                                                   @TokenToSystemUser SystemUser loginSystemUser){
        SystemUserAddress systemUserAddress = whitesMealUserAddressService.getSystemUserAddressById(updateSystemUserAddressParam.getAddressId());
        // 判断要修改的不是本用户的id的if条件句
        if (!loginSystemUser.getUserId().equals(systemUserAddress.getUserId())){
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        SystemUserAddress updateSystemUserAddress = new SystemUserAddress();
        BeanUtil.copyProperties(updateSystemUserAddressParam,updateSystemUserAddress);
        updateSystemUserAddress.setUserId(loginSystemUser.getUserId());
        Boolean updateResult = whitesMealUserAddressService.updateSystemUserAddress(updateSystemUserAddress);
        if (updateResult){
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }

    @GetMapping("/address/{addressId}")
    @ApiOperation(value = "获取收货地址详情", notes = "传参为地址id")
    public Result<WhitesMealUserAddressVO> getSystemUserAddress(@PathVariable("addressId") Long addressId,
                                                                @TokenToSystemUser SystemUser loginSystemUser){
        SystemUserAddress systemUserAddress = whitesMealUserAddressService.getSystemUserAddressById(addressId);
        WhitesMealUserAddressVO whitesMealUserAddressVO = new WhitesMealUserAddressVO();
        BeanUtil.copyProperties(systemUserAddress, whitesMealUserAddressVO);
        // 判断要修改的不是本用户的id的if条件句
        if (!loginSystemUser.getUserId().equals(systemUserAddress.getUserId())){
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        return ResultGenerator.genSuccessResult(whitesMealUserAddressVO);
    }

    @GetMapping("/address/default")
    @ApiOperation(value = "获取默认收货地址", notes = "无传参")
    public Result getDefaultSystemUserAddress(@TokenToSystemUser SystemUser loginSystemUser){
        SystemUserAddress systemUserAddress = whitesMealUserAddressService.getDefaultAddressByUserId(loginSystemUser.getUserId());
        return ResultGenerator.genSuccessResult(systemUserAddress);
    }

    @DeleteMapping("/address/{addressId}")
    @ApiOperation(value = "删除收货地址", notes = "传参为地址id")
    public Result deleteAddress(@PathVariable("addressId") Long addressId,
                                @TokenToSystemUser SystemUser loginSystemUser){
        SystemUserAddress systemUserAddress = whitesMealUserAddressService.getSystemUserAddressById(addressId);
        // 判断要修改的不是本用户的id的if条件句
        if (!loginSystemUser.getUserId().equals(systemUserAddress.getUserId())){
            return ResultGenerator.genFailResult(ServiceResultEnum.REQUEST_FORBIDEN_ERROR.getResult());
        }
        Boolean deleteResult = whitesMealUserAddressService.deleteById(addressId);
        if (deleteResult){
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult(ServiceResultEnum.OPERATE_ERROR.getResult());
    }

}
